package org.example.test;

import org.example.entity.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class NewDuiXiang {
    public static void main(String[] args) {
        // bean 对象创建的四种方式

        // 方式一:
        /*
        *
        *   <bean id="user1" class="org.example.entity.User"/>
        *   找无参构造方法造bean,相当于 User user = new User;
        *
        * */
        //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        //User user = (User) context.getBean("user1");
        //System.out.println(user);

        // 方式二:
        /*
        *
        *   通过指定的静态工厂，为我们造bean(直接使用静态工厂的静态方法创建对象)
        *   <bean id="user2" class="org.example.factory.UserStaticFactory" factory-method="getUser" />
        *   相当于 User user = UserStaticFactory.getUser();
        *
        * */
        //ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        //User user = (User) context.getBean("user2");
        //System.out.println(user);

        // 方式三:
        /*
        *
        *   通过指定的非静态工厂,为我们造bean(先创建非静态工厂然后再通过工厂的非静态方法创建对象)
        *   <bean id="userFactory" class="org.example.factory.UserFactory"/>
        *   UserFactory userFactory = new UserFactory();
            <bean id="user3" factory-bean="userFactory" factory-method="getUser"/>
            User user3 = userFactory.getUser();
        *
        * */
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
        User user = (User) context.getBean("user3");
        System.out.println(user);
    }
}
